#define _CRT_SECURE_NO_WARNINGS 1
#include"TBS.h"

int Menu()
{
	int input = 0;
	printf("\t\t\t=============================================================\n");
	printf("\t\t\t*************************Main Menu***************************\n");
	printf("\t\t\t=============================================================\n");
	printf("\t\t\t              Ticket-selling Service ------ 1 \n");
	printf("\t\t\t              System Management      ------ 2 \n");
	printf("\t\t\t              Log Out                ------ 3 \n");
	printf("\t\t\t=============================================================\n");
	printf("\t\t\tPlease choose:>");
	scanf("%d", &input);

	return input;

}

int check_capacity(Station* p)
{
	if (p->size == p->capacity) //need to be expanded
	{
		StationInfo* temp = (StationInfo*)realloc(p->data, sizeof(StationInfo) * (p->capacity + INC_SIZE));
		if (temp == NULL)
		{
			perror("check_capacity");
			return 0; //expanded failed
		}
		else
		{
			p->data = temp;
			p->capacity += INC_SIZE;
			return 1; //expanded successed
		}
	}

	return 1; //no need to be expanded
}

void LoadData(Station* p)
{
	FILE* pf = fopen("StationData.dat", "rb");
	if (pf == NULL)
	{
		perror("LoadData");
		return;
	}

	StationInfo temp = { 0 };
	while (fread(&temp, sizeof(StationInfo), 1, pf))
	{
		check_capacity(p);

		p->data[p->size] = temp;
		p->size++;
	}

	fclose(pf);
	pf = NULL;
}

void InitialData(Station* p)
{
	p->data = (StationInfo*)malloc(sizeof(StationInfo) * DEFAULT_CAPACITY);
	if (p->data == NULL)
	{
		perror("InitialData");
		return;
	}

	p->capacity = DEFAULT_CAPACITY;
	p->size = 0;

	int input = 0;
	printf("\t\t\t-----------------------System Notice-------------------------\n");
	printf("\t\t\tIf you want to load data from the local file, please input 1,\n\t\t\telse, input other numbers except 1.\n");
	printf("\t\t\t-------------------------------------------------------------\n\t\t\tPlease choose:>");
	scanf("%d", &input);
	if (input == 1)
	{
		LoadData(p);
		printf("\n\t\t\t------------------------System Notice------------------------\n");
		printf("\t\t\t[-------------------Data has been loaded-------------------]\n");
		printf("\t\t\t-------------------------------------------------------------\n");
	}
}

void SaveData(Station* p)
{
	FILE* pf = fopen("StationData.dat", "wb");
	if (pf == NULL)
	{
		perror("SaveData");
		return;
	}

	for (int i = 0; i < p->size; i++)
	{
		fwrite(p->data + i, sizeof(StationInfo), 1, pf);
	}

	fclose(pf);
	pf = NULL;

	printf("\n\t\t\t------------------------System Notice------------------------\n");
	printf("\t\t\t[--------------------Data has been saved--------------------]\n");
	printf("\t\t\t-------------------------------------------------------------\n\n\n");
}

void FreeMemory(Station* p)
{
	free(p->data);
	p->data = NULL;
}

void ExitProgram(Station* p)
{
	SaveData(p);

	FreeMemory(p);

	exit(0);
}


/*Functions for TicketSelling*/

int TicketSellingMenu()
{
	int input = 0;
	printf("\t\t\t=============================================================\n");
	printf("\t\t\t**********************Ticket Selling Menu********************\n");
	printf("\t\t\t=============================================================\n");
	printf("\t\t\t              Inquire Ticket         ------ 1 \n");
	printf("\t\t\t              Purchase Ticket        ------ 2 \n");
	printf("\t\t\t              Return To Previous Menu ----- 3 \n");
	printf("\t\t\t=============================================================\n");
	printf("\t\t\tPlease choose:>");
	scanf("%d", &input);

	return input;
}

int FindByID(Station* p, char id[])
{
	for (int i = 0; i < p->size; i++)
	{
		if (strcmp(p->data[i].stationID, id) == 0)
		{
			return i;
		}
	}
	return -1;
}

void InquireTicket(Station* p)
{
	char start1[STR_LEN] = { 0 };
	char end1[STR_LEN] = { 0 };
	printf("Please input start point:>");
	scanf("%s", start1);
	printf("Please input end point:>");
	scanf("%s", end1);

	for (int i = 0; i < p->size; i++)
	{
		if (strcmp(p->data[i].start, start1) == 0
			 && strcmp(p->data[i].end, end1) == 0)
		{
			printf("Tickets was found:");
			printf("\n========================================================================================\n");
			printf("ID\tStart\t\tEnd\t\tPrice\tDepartment Time\t\t\tNumber\n");
			printf("========================================================================================\n");
			printf("%s\t%s\t\t%s\t\t%d\t%s\t\t\t%d\n\n\n", p->data[i].stationID, p->data[i].start, p->data[i].end, p->data[i].price, p->data[i].time,
				p->data[i].numbers);
			return;
		}
	}

	//not found
	printf("\n\t\t\t------------------------System Notice------------------------\n");
	printf("\t\t\t[---------------Sorry!No such train avaliable---------------]\n");
	printf("\t\t\t-------------------------------------------------------------\n\n\n");
}

void PurchaseTicket(Station* p, BuyInfo* q)
{
	char id[STR_LEN] = { 0 };
	printf("Please input the StationID you want to buy:>");
	scanf("%s", id);

	int buyNum = 0;
	int pos = FindByID(p, id);
	if (pos != -1)
	{
		printf("Please fill in the ticket information:>\n");
		printf("--------------------------------------------------------\n");
		printf("\t\t\t1.Number of purchase ticket:>");
		scanf("%d", &buyNum);
		if (buyNum > p->data[pos].numbers)
		{
			printf("\n\t\t\t------------------------System Notice------------------------\n");
			printf("\t\t\t[------------------Tickets are not enough------------------]\n");
			printf("\t\t\t-------------------------------------------------------------\n\n");
			return;
		}
		printf("\t\t\t2.Ticket purchaser's name:>");
		scanf("%s", q->data[q->size].clientName);
		printf("--------------------------------------------------------\n");
	}
	else
	{
		printf("\n\nNOTICE:Station[%s] was not found!\n\n", id);
		return;
	}

	//update ticket numbers
	p->data[pos].numbers -= buyNum;

	strcpy(q->data[q->size].stationID, p->data[pos].stationID);
	strcpy(q->data[q->size].start, p->data[pos].start);
	strcpy(q->data[q->size].end, p->data[pos].end);
	q->data[q->size].price = p->data[pos].price;
	strcpy(q->data[q->size].time, p->data[pos].time);
	q->data[q->size].numbers = buyNum;

	q->size++;

	printf("Purchase Successfully!\n");
}

void TicketSelling(Station* p, BuyInfo* q)
{
	int input = 0, flag = 0;

	while (1)
	{
		input = TicketSellingMenu();
		switch (input)
		{
		case 1:
			InquireTicket(p);
			break;
		case 2:
			PurchaseTicket(p, q);
			break;
		case 3:
			flag = 1;
			break;
		default :
			printf("Invalid input!");
			break;
		}

		if (flag == 1)
		{
			break;
		}
	}
}

/*Functions for SystemManage*/
int VerifyIdentity()
{
	char key[PASSWORD_LEN] = { 0 };
	printf("Please input the management password:>");
	scanf("%s", key);

	if (strcmp(key, KEY) == 0)
	{
		return 1;
	}

	return 0;
}

int SystemManageMenu()
{
	int input = 0;
	printf("\t\t\t=============================================================\n");
	printf("\t\t\t********************System Management Menu*******************\n");
	printf("\t\t\t=============================================================\n");
	printf("\t\t\t              Display All Satations  ------ 1 \n");
	printf("\t\t\t              Add New Stations       ------ 2 \n");
	printf("\t\t\t              Modify Stations        ------ 3 \n");
	printf("\t\t\t              Delete Stations        ------ 4 \n");
	printf("\t\t\t              Show Purchase          ------ 5 \n");
	printf("\t\t\t              Return To Previous Menu ----- 6 \n");
	printf("\t\t\t=============================================================\n");
	printf("\t\t\tPlease choose:>");
	scanf("%d", &input);

	return input;
}

void PrintStations(Station* p)
{
	printf("\n========================================================================================\n");
	printf("ID\tStart\t\tEnd\t\tPrice\tDepartment Time\t\t\tNumber\n");
	printf("========================================================================================\n");
	for (int i = 0; i < p->size; i++)
	{

		printf("%s\t%s\t\t%s\t\t%d\t%s\t\t\t%d\n", p->data[i].stationID, p->data[i].start, p->data[i].end, p->data[i].price, p->data[i].time,
			p->data[i].numbers);
	}
	printf("\n\n\n");
}

void AddStations(Station* p)
{
	int num;
	printf("Please input the numbers of the stations:>");
	scanf("%d", &num);

	for (int i = 0; i < num; i++)
	{
		check_capacity(p);

		printf("\nPlease enter the information of the %d/%d stations:\n", i + 1, num);
		printf("----------------------------------------------------\n");

		//input stations ID
		printf("\t\t1.ID:");
		scanf("%s", p->data[p->size].stationID);

		//input start point
		printf("\t\t2.Start point:");
		scanf("%s", p->data[p->size].start);

		//input end point
		printf("\t\t3.End point:");
		scanf("%s", p->data[p->size].end);

		//input price
		printf("\t\t4.Ticket Price:");
		scanf("%d", &(p->data[p->size].price));

		//input depart time
		printf("\t\t5.Department time(year-month-day-hour:minute):");
		scanf("%s", p->data[p->size].time);

		//input number
		printf("\t\t6.Numbers:");
		scanf("%d", &(p->data[p->size].numbers));

		p->size++;
		printf("\nADDED SUCESSFULLY!\n");
		printf("----------------------------------------------------\n");
	}

}

void ModifyStations(Station* p)
{
	char id[STR_LEN];

	printf("Please enter the ID of the station you want to modify:>");
	scanf("%s", id);

	int pos = FindByID(p, id);
	if (pos != -1)
	{
		printf("\nPlease input the new data of station[%s]\n", id);
		printf("----------------------------------------------------\n");
		//input stations ID
		printf("\t\t1.ID:");
		scanf("%s", p->data[pos].stationID);

		//input start point
		printf("\t\t2.Start point:");
		scanf("%s", p->data[pos].start);

		//input end point
		printf("\t\t3.End point:");
		scanf("%s", p->data[pos].end);

		//input price
		printf("\t\t4.Ticket Price:");
		scanf("%d", &(p->data[pos].price));

		//input depart time
		printf("\t\t5.Department time(year-month-day-hour:minute):");
		scanf("%s", p->data[pos].time);

		//input number
		printf("\t\t6.Numbers:");
		scanf("%d", &(p->data[pos].numbers));
		printf("----------------------------------------------------\n");
	}
	else
	{
		printf("NOTICE:Station[%s] was not found!\n\n", id);
	}
}

void DeleteStations(Station* p)
{
	char id[STR_LEN];

	printf("Please enter the ID of the station you want to delete:>");
	scanf("%s", id);

	int pos = FindByID(p, id);
	if (pos != -1)
	{
		for (int i = pos; i < p->size - 1; i++)
		{
			p->data[i] = p->data[i + 1];
		}
		p->size--;
		printf("Station[%s] was successfully deleted!\n", id);
	}
	else
	{
		printf("NOTICE:Station[%s] was not found!\n\n", id);
	}
	
}

void ShowPurchase(BuyInfo* q)
{
	printf("\n===============================================================================================================\n");
	printf("Purchaser's Name\tID\tStart\t\tEnd\t\tPrice\tDepartment Time\t\t\tNumber\n");
	printf("===============================================================================================================\n");
	for (int i = 0; i < q->size; i++)
	{
		printf("%s\t\t\t%s\t%s\t\t%s\t\t%d\t%s\t\t\t%d\n", q->data[i].clientName, q->data[i].stationID, q->data[i].start
			,q->data[i].end, q->data[i].price, q->data[i].time, q->data[i].numbers);
	}
	printf("\n\n\n");
}


void SystemManage(Station* p, BuyInfo* q)
{
	int flag = 0;
	if (VerifyIdentity())
	{
		int input = 0;

		while (1)
		{
			input = SystemManageMenu();

			switch (input)
			{
				case 1:
					PrintStations(p);
					break;
				case 2:
					AddStations(p);
					break;
				case 3:
					ModifyStations(p);
					break;
				case 4:
					DeleteStations(p);
					break;
				case 5:
					ShowPurchase(q);
					break;
				case 6:
					flag = 1;
					break;

				default:
					printf("Invalid input!\n");
					break;
			}
			if (flag == 1)
			{
				return;
			}
		}
	}
	else
	{
		printf("\n\t\t\t------------------------System Notice------------------------\n");
		printf("\t\t\t[------------------------Password Wrong!--------------------]\n");
		printf("\t\t\t-------------------------------------------------------------\n\n\n");
	}
}

